package com.scx.controller.user;

import com.scx.entity.Book;
import com.scx.result.Result;
import com.scx.service.BookService;
import com.scx.vo.BookVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userBookController")
@RequestMapping("/user/Book")
@Api("C端-图书浏览接口")
@Slf4j
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping("/{id}")
    @ApiOperation("根据id查询图书")
    @Cacheable(cacheNames = "BookCache",key = "#id")
    public Result<Book> queryBookById(@PathVariable("id") Long id) {
        log.info("根据id查询图书:{}",id);
        Book book = bookService.queryById(id);
        return Result.success(book);
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查询图书")
    @Cacheable(cacheNames = "BookCache",key = "#categoryId")
    public Result<List<BookVO>> list(Long categoryId) {
        log.info("根据分类id查询图书:{}",categoryId);
        List<BookVO> BookVOList = bookService.listWithGifts(categoryId);
        return Result.success(BookVOList);
    }

    @GetMapping("/top")
    @ApiOperation("获取图书热销Top10")
    @Cacheable(cacheNames = "TopCache",key = "#categoryId")
    public Result<List<Book>> top10() {
        log.info("查询图书Top10");
        List<Book> bookTop = bookService.top10();
        return Result.success(bookTop);
    }
}
